package org.qing.slice.excel;

import java.io.File;
import java.io.PrintStream;
import java.math.BigDecimal;

import org.qing.javafx.console.Console;
import org.qing.javafx.controller.Controller;
import org.qing.slice.excel.utils.NumberUtil;
import org.qing.slice.excel.utils.SliceUtil;
import org.qing.slice.excel.utils.StatusUtil;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;

@SuppressWarnings({ "restriction" })
public class MainController extends Controller {

	@FXML
	private TextField limitInput;

	@FXML
	private TextField srcFileInput;

	@FXML
	private Button sliceButton;

	@FXML
	private ProgressBar progressFileBar;
	
    @FXML
    private TextArea console;
    
	@FXML
	protected void selectExcelAction(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("请选择要分割的Excel文件");
		File fileChooserDir = fileChooser.showOpenDialog(this.getuIContext().getStage());
		if (fileChooserDir != null) {
			srcFileInput.setText(fileChooserDir.getAbsolutePath());
		} else {
			Alert alert = new Alert(Alert.AlertType.INFORMATION);
			alert.setTitle("提示");
			alert.setHeaderText(null);
			alert.setContentText("请选择要分割的Excel文件");
			alert.showAndWait();
		}
	}

	@FXML
	protected void slice(ActionEvent event) {
		String limitStr = limitInput.getText();
		if(limitStr==null||"".equals(limitStr)) {
			Alert alert = new Alert(Alert.AlertType.INFORMATION);
			alert.setTitle("提示");
			alert.setHeaderText(null);
			alert.setContentText("请输入行数限制");
			alert.showAndWait();
			return;
		}
		if(!NumberUtil.isNumeric(limitStr)) {
			Alert alert = new Alert(Alert.AlertType.INFORMATION);
			alert.setTitle("提示");
			alert.setHeaderText(null);
			alert.setContentText("行数限制必须是数字");
			alert.showAndWait();
			return;
		}
		String srcFile = srcFileInput.getText();
		if(srcFile==null||"".equals(srcFile)) {
			Alert alert = new Alert(Alert.AlertType.INFORMATION);
			alert.setTitle("提示");
			alert.setHeaderText(null);
			alert.setContentText("请选文件");
			alert.showAndWait();
			return;
		}
		
		Integer limit = new BigDecimal(limitStr).intValue();
		if(StatusUtil.status) {
			Alert alert = new Alert(Alert.AlertType.WARNING);
			alert.setTitle("提示");
			alert.setHeaderText(null);
			alert.setContentText("正在处理"+StatusUtil.src.getName()+"文件");
			alert.showAndWait();
			return;
		}
		synchronized (StatusUtil.class) {
			try {
				StatusUtil.status = true;
				StatusUtil.limit = limit;
				StatusUtil.src = new File(srcFile);
				String tmpFile = SliceUtil.slice();
				SliceUtil.openFileLocation(tmpFile);
			}catch (Exception e) {
				e.printStackTrace();
				Alert alert = new Alert(Alert.AlertType.ERROR);
				alert.setTitle("提示");
				alert.setHeaderText(null);
				alert.setContentText(StatusUtil.src.getName()+"文件处理失败");
				alert.showAndWait();
			}
			StatusUtil.src = null;
			StatusUtil.status = false;
		}
		
	}

	@Override
	public void init() {
		// 初始化服务器访问地址
		limitInput.setText(String.valueOf(5000));
		StatusUtil.progressFileBar = progressFileBar;
		StatusUtil.status = false;
		PrintStream printStream = new PrintStream(new Console(console));
        System.setOut(printStream);
        System.setErr(printStream);
	}
}
